package services;

import dao.ArticleInfoDao;
import models.ArticleInfo;
import models.UserInfo;
import utils.ResultUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/9
 * Time:17:49
 **/
@WebServlet("/myart")
public class MyArtServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //定义
        int succ = -1;
        String msg = "";
        List<ArticleInfo> list = null;
        //定义返回给前端的参数
        //业务//显示文章列表//查询数据库
        HttpSession session = req.getSession(false);
        if(session == null){
            //未登录
            msg="请先登录！";
        }else{
            //已登录
            UserInfo userInfo = (UserInfo) session.getAttribute("userinfo");//用于获取与指定名字相联系的属性值//userinfo:要获取的属性名称
            //查询数据库
            int uid = userInfo.getId();
            ArticleInfoDao articleInfoDao = new ArticleInfoDao();
            try {
                list = articleInfoDao.getListByUID(uid);
                succ = 1;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        //返回结果
        HashMap<String,Object> res = new HashMap<>();
        res.put("succ",succ);
        res.put("msg",msg);
        res.put("list",list);
        ResultUtils.write(resp,res);
    }
}
